package com.zjhn.ds.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.zjhn.ds.common.BizResult;
import com.zjhn.ds.domain.dto.pad.ControlInfoDto;
import com.zjhn.ds.domain.dto.pad.UserInfoDto;
import com.zjhn.ds.domain.entity.erp.Operator;
import com.zjhn.ds.domain.entity.erp.ProTask;
import com.zjhn.ds.domain.entity.jlw.*;
import com.zjhn.ds.mapper.*;
import com.zjhn.ds.param.ControlInfoParam;
import com.zjhn.ds.service.HomePageService;
import com.zjhn.ds.utils.CommonUtil;
import com.zjhn.ds.utils.UserUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * @BelongsProject: ds_jlw
 * @BelongsPackage: com.zjhn.ds.service.impl
 * @Author: sxt
 * @CreateTime: 2023-09-21  10:05
 * @Description:
 * @Version: 1.0
 */
@Service
public class HomePageServiceImpl implements HomePageService {

    @Resource
    private UserUtil userUtil;

    @Resource
    private CommonUtil commonUtil;

    @Resource
    private SpinnerMapper spinnerMapper;

    @Resource
    private WorkshopMapper workshopMapper;

    @Resource
    private ProTaskMapper proTaskMapper;

    @Resource
    private StationMapper stationMapper;

    @Resource
    private GroupSpinnerMapper groupSpinnerMapper;

    @Resource
    private GroupStationMapper groupStationMapper;

    @Resource
    private OperatorMapper operatorMapper;

    /**
     * 登录后获取用户数据
     * @return
     */
    @Override
    public BizResult<UserInfoDto> getUserInfo() {
        String abpUserId = userUtil.getUserInfo().getAbpUserId();
        Operator operator = operatorMapper.selectOne(
                new QueryWrapper<Operator>()
                        .eq("id", abpUserId)
                        .eq("IsDeleted", 0)
        );
        if (operator == null)
            return BizResult.fail("用户不存在");

        List<Workshop> workshops = workshopMapper.selectList(new QueryWrapper<Workshop>().eq("director_id", operator.getId()));
        if (workshops.size() > 1)
            return BizResult.fail("当前用户为多个车间的车间主任,无法获取准确信息");

        UserInfoDto res = UserInfoDto.builder()
                .operateId(operator.getId())
                .username(operator.getUsername())
                .userNo(operator.getUserNo())
                .realName(operator.getRealName())
                .department(commonUtil.getDepartmentId(operator.getDepartmentGuid()))
                .image(operator.getImage())
                .build();
        if (workshops.size() == 1) {
            // 车间主任
            res.setType(2);
            res.setWorkshopId(workshops.get(0).getId());
            res.setWorkshop(workshops.get(0).getName());
        } else {
            // 挡车工
            Spinner spinner = spinnerMapper.selectOne(new QueryWrapper<Spinner>().eq("operate_id", operator.getId()));
            if (spinner == null)
                return BizResult.fail("当前用非车间主任或挡车工，无法获取信息");

            res.setType(1);
            res.setWorkshopId(spinner.getWorkshopId());
            res.setWorkshop(commonUtil.getWorkshopById(spinner.getWorkshopId()));
            res.setSpinnerId(spinner.getId());
        }
        return BizResult.success(res);
    }

    /**
     * 首页控制台数据
     * @param param
     * @return
     */
    @Override
    public BizResult<ControlInfoDto> controlInfo(ControlInfoParam param) {
        Integer all = proTaskMapper.selectCount(new QueryWrapper<ProTask>().eq("DeleterId", 0));
        Integer finish = proTaskMapper.selectCount(
                new QueryWrapper<ProTask>().eq("is_finish",1).eq("DeleterId", 0));
        ControlInfoDto res = ControlInfoDto.builder()
                .proTaskQty((all - finish))
                .finishQty(finish)
                .spinnerQty(0)
                .stationQty(0)
                .build();
        if (StringUtils.isNotBlank(param.getSpinnerId())) {
            // 挡车工
            GroupSpinner groupSpinner = groupSpinnerMapper.selectOne(new QueryWrapper<GroupSpinner>().eq("spinner_id", param.getSpinnerId()));
            if (groupSpinner != null) {
                List<GroupStation> groupStations = groupStationMapper.selectList(new QueryWrapper<GroupStation>().eq("group_id", groupSpinner.getGroupId()));
                res.setStationQty(groupStations.size());
            }
        }else {
            // 车间主任
            Integer stationQty = stationMapper.selectCount(new QueryWrapper<Station>().eq("workshop_id", param.getWorkshopId()));
            res.setStationQty(stationQty);

            Integer spinnerQty = spinnerMapper.selectCount(new QueryWrapper<Spinner>().eq("workshop_id", param.getWorkshopId()));
            res.setStationQty(spinnerQty);
        }
        return BizResult.success(res);
    }
}
